package com.ciwong.epaper.modules.homework.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.ciwongwrite.utils.CWUtils;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.homework.bean.PublishDKWorkRecords;
import com.ciwong.epaper.modules.homework.bean.TodayDate;
import com.ciwong.mobilelib.i.ViewOnClickListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: wyk
 * @time: 2021/10/8 11:27
 */
public class DkWorkAdapter extends BaseAdapter {

    /**
     * 添加类型：全部
     */
    public static final int ADD_TYPE_ALL = 0;

    /**
     * 添加类型：已添加
     */
    public static final int ADD_TYPE_YES = 1;

    /**
     * 添加类型：未添加
     */
    public static final int ADD_TYPE_NO = 2;

    /**
     * 添加类型
     */
    private int addType;

    private List<PublishDKWorkRecords> mPublishDKWorkRecords;

    private Context mContext;

    private String[] cheStr = {"一", "二", "三", "四", "五", "六", "七"};
    private String[] numberStr = {"1", "2", "3", "4", "5", "6", "7"};

    public DkWorkAdapter(Context context, List<PublishDKWorkRecords> publishDKWorkRecords) {
        this.mContext = context;
        this.mPublishDKWorkRecords = publishDKWorkRecords;
    }

    @Override
    public int getCount() {
        return null == mPublishDKWorkRecords ? 0 : mPublishDKWorkRecords.size();
    }

    @Override
    public Object getItem(int i) {
        return null == mPublishDKWorkRecords ? null : mPublishDKWorkRecords.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.dk_work_list_par, null);
            viewHolder = new ViewHolder();
            viewHolder.dk_rule = view.findViewById(R.id.dk_rule);
            viewHolder.dk_work_status = view.findViewById(R.id.dk_work_status);
            viewHolder.publish_work_date = view.findViewById(R.id.publish_work_date);
            viewHolder.publish_work_name = view.findViewById(R.id.publish_work_name);
            viewHolder.publish_work_member = view.findViewById(R.id.publish_work_member);
            viewHolder.work_details = view.findViewById(R.id.work_details);
            viewHolder.work_effectiveDate = view.findViewById(R.id.work_effectiveDate);
            viewHolder.work_details_layout = view.findViewById(R.id.work_details_layout);
            viewHolder.dk_work_status_lay = view.findViewById(R.id.dk_work_status_lay);
            viewHolder.dk_days_text = view.findViewById(R.id.dk_days_text);
            viewHolder.dk_num_text = view.findViewById(R.id.dk_num_text);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        setView(i, viewHolder);
        return view;
    }


    /**
     * 设置页面数据
     *
     * @param postion
     * @param viewHolder
     */
    private void setView(int postion, ViewHolder viewHolder) {
        try {
            if (null != mPublishDKWorkRecords && mPublishDKWorkRecords.size() > postion) {
                final PublishDKWorkRecords publishDKWorkRecords = mPublishDKWorkRecords.get(postion);
                long effectiveDate = publishDKWorkRecords.getEffectiveDateTim() * 1000;
                long sendDate = publishDKWorkRecords.getSendDateTim() * 1000;
                //作业是否开始1：开始，0：未开始
                int isStart = publishDKWorkRecords.getIsStart();
                //需要打卡的天数
                int dkdays = publishDKWorkRecords.getDkdays();
                //总人数
                int totalNum = publishDKWorkRecords.getTotalNum();


                if (0 != sendDate) {
                    //设置作业开始时间
                    viewHolder.publish_work_date.setText(CWUtils.formatDate3(sendDate));
                }
                if (isStart == 1) {
                    //打卡人数
                    int dkNum = 0;
                    //第几天打卡
                    int dkDateNum = 0;
                    TodayDate todayDate = publishDKWorkRecords.getTodayDate();
                    if (null != todayDate) {
                        //打卡人数
                        dkNum = todayDate.getDkNum();
                        //第几天打卡
                        dkDateNum = todayDate.getDkDateNum();
                    }
                    viewHolder.dk_days_text.setVisibility(View.VISIBLE);
                    viewHolder.dk_num_text.setVisibility(View.VISIBLE);
                    viewHolder.dk_days_text.setText(dkDateNum + 1 + "/" + dkdays);
                    String dk_num = mContext.getResources().getString(R.string.dk_num);
                    String format = String.format(dk_num, dkNum, totalNum);
                    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(format);
                    ForegroundColorSpan span = new ForegroundColorSpan(mContext.getResources().getColor(R.color.title_color));
                    if (dkNum >= 10) {
                        stringBuilder.setSpan(span, 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        stringBuilder.setSpan(span, 4, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    viewHolder.dk_num_text.setText(stringBuilder);
                } else {
                    viewHolder.dk_days_text.setVisibility(View.GONE);
                    viewHolder.dk_num_text.setVisibility(View.GONE);
                }
                //班级名
                viewHolder.publish_work_member.setText(mContext.getString(R.string.class_name) + publishDKWorkRecords.getClassName());
                viewHolder.publish_work_name.setText(publishDKWorkRecords.getWorkName());
                viewHolder.work_effectiveDate.setText(mContext.getString(R.string.effective_date) + CWUtils.formatDate3(effectiveDate));
                long nowTime = System.currentTimeMillis();
                if (nowTime > effectiveDate) {
                    viewHolder.dk_work_status.setText("已结束");
                    viewHolder.dk_work_status.setTextColor(mContext.getResources().getColor(R.color.tea_work_eff_time));
                    viewHolder.dk_work_status_lay.setBackground(mContext.getResources().getDrawable(R.drawable.dk_work_status));
                } else if (nowTime < effectiveDate && nowTime > sendDate) {
                    viewHolder.dk_work_status.setText("进行中");
                    viewHolder.dk_work_status.setTextColor(mContext.getResources().getColor(R.color.white));
                    viewHolder.dk_work_status_lay.setBackground(mContext.getResources().getDrawable(R.drawable.dk_work_in_progress));
                } else if (nowTime < effectiveDate && nowTime < sendDate && sendDate != 0) {
                    viewHolder.dk_work_status.setText("未开始");
                    viewHolder.dk_work_status.setTextColor(mContext.getResources().getColor(R.color.red));
                    viewHolder.dk_work_status_lay.setBackground(mContext.getResources().getDrawable(R.drawable.dk_work_status));
                }

                String dkRule = publishDKWorkRecords.getDkRule();
                String[] split = dkRule.split(",");
                StringBuffer sb = new StringBuffer();
                if (split.length == 7) {
                    viewHolder.dk_rule.setText(mContext.getString(R.string.dk_rule_every_day));
                } else {
                    for (int i2 = 0; i2 < split.length; i2++) {
                        String s = split[i2];
                        int indext = Arrays.binarySearch(numberStr, s);
                        if (i2 == split.length - 1) {
                            sb = sb.append(cheStr[indext]);
                        } else {
                            sb = sb.append(cheStr[indext] + ", ");
                        }

                    }
                    viewHolder.dk_rule.setText(mContext.getString(R.string.dk_rule) + sb.toString());
                }

                viewHolder.work_details.setText(publishDKWorkRecords.getWorkDetails());

                viewHolder.work_details_layout.setOnClickListener(new ViewOnClickListener() {
                    @Override
                    public void avertRepeatOnClick(View view) {
                        EpaperJumpManager.jumpToDKWorkDetails((Activity) mContext, publishDKWorkRecords.getWorkId(), publishDKWorkRecords.getClassName(), R.id.go_back);
                    }
                });
            }
        } catch (Exception e) {

        }

    }

    /**
     * 清空数据
     */
    public void clear() {
        if (mPublishDKWorkRecords != null) {
            mPublishDKWorkRecords.clear();
            //notifyDataSetChanged();
        }
    }

    public void addAll(List<PublishDKWorkRecords> pageList) {
        if (this.mPublishDKWorkRecords == null) {
            this.mPublishDKWorkRecords = new ArrayList<PublishDKWorkRecords>();
        }
        if (pageList != null) {
            switch (addType) {
                case ADD_TYPE_ALL:
                    this.mPublishDKWorkRecords.addAll(pageList);
                    break;

                case ADD_TYPE_YES:
                    for (PublishDKWorkRecords publishWorkRecords : pageList) {
//                        if (epaperInfo.isAdded()) {
                        this.mPublishDKWorkRecords.add(publishWorkRecords);
//                        }
                    }
                    break;

                default:
                    for (PublishDKWorkRecords publishWorkRecords : pageList) {
//                        if (epaperInfo.isAdded()) {
                        this.mPublishDKWorkRecords.add(publishWorkRecords);
//                        }
                    }
                    break;
            }
            notifyDataSetChanged();
        }
    }

    private class ViewHolder {
        private TextView publish_work_date;
        private TextView publish_work_name;
        private TextView dk_work_status;
        private TextView publish_work_member;
        private TextView work_details;
        private TextView work_effectiveDate;
        private TextView dk_rule;
        private RelativeLayout work_details_layout;
        private RelativeLayout dk_work_status_lay;
        private TextView dk_days_text;
        private TextView dk_num_text;

    }
}
